package com.peng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.common.R;
import com.peng.entity.Employee;
import com.peng.service.EmployeeService;
import javafx.geometry.Pos;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.digester.Digester;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String username = employee.getUsername();
        String s = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,username);
        Employee one = employeeService.getOne(queryWrapper);

        if (one==null){
            return R.error("当前用户不存在");
        }
        if (!one.getPassword().equals(s))
            return R.error("密码错误");
        if (one.getStatus()==0){
            return R.error("账户已锁定");
        }
        request.getSession().setAttribute("employeeId",one.getId());

        return R.success(one);
    }

    /**
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<Employee> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employeeId");
        return R.success(null);
    }

    @PostMapping
    public R<String> saveEmployee(@RequestBody Employee employee,HttpServletRequest request){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(new Date());
//        employee.setUpdateTime(new Date());
//        Long employeeId = (Long) request.getSession().getAttribute("employeeId");
//        employee.setCreateUser(employeeId);
//        employee.setUpdateUser(employeeId);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    //employee/page?page=1&pageSize=10&name=asd
    @GetMapping("/page")
    public R<IPage<Employee>> pageR(Integer page,Integer pageSize,String name){
        IPage page1= employeeService.pageR(page,pageSize,name);
        return R.success(page1);
    }

    @PutMapping
    public R update(@RequestBody Employee employee,HttpServletRequest request){
//        employee.setUpdateTime(new Date());
//
//        Long employeeId = (Long) request.getSession().getAttribute("employeeId");
//        employee.setUpdateUser(employeeId);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") Long id){
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }
}
